package com.cy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.common.exception.ServiceException;
import com.cy.common.vo.Carts;
import com.cy.dao.SysCartDao;
import com.cy.dao.SysGoodsListDao;
import com.cy.entity.SysCarts;
import com.cy.entity.SysGoodsSpecs;
import com.cy.service.SysCartService;

/**  
 * @Title:  SysCartServiceImpl.java   
 * @Package com.cy.service.impl   
 * @Description:    描述   
 * @author: ZHANGSIMIN     
 * @date:   2019年10月22日 上午9:51:17   
 * @version V1.3 
 */
@Service
public class SysCartServiceImpl implements SysCartService{

	@Autowired
	private SysCartDao sysCartDao;
	
	@Autowired
	private SysGoodsListDao sysGoodsListDao;
	
	@Override
	public List<Carts> getCartsByUserid(Integer id) {
		List<SysCarts> cartsList = sysCartDao.getCartsByUserid(id);
		List<Carts> list = new ArrayList<>();
		for(SysCarts cart : cartsList) {
			SysGoodsSpecs specInfo = sysCartDao.getSpecInfo(cart.getSpecs_id());
			String goodsListImg = sysGoodsListDao.getGoodsListImg(specInfo.getGoods_id());
			Carts item = new Carts();
			item.setCart_id(cart.getId());
			item.setCount(cart.getCount());
			item.setGoods_id(specInfo.getGoods_id());
			item.setSpecs_info(cart.getSpecs_info());
			item.setPrice(specInfo.getGoods_price());
			item.setImgUrl(goodsListImg);
			list.add(item);
		}
		return list;
	}

	@Override
	public Integer deleteCartsByCartId(Integer id) {
		if(id == null || id == 0) {
			throw new ServiceException("参数错误");
		}
		Integer rows = sysCartDao.deleteCartsByCartId(id);
		return rows;
	}

}
